#include "phys_engine.h"
#include "phys_solve_math.h"
#include <graphics.h>
#include <cstdlib>
#include <vector>
#include <windef.h>
using namespace std;
using namespace phys_engine;

long long tick_cnt=0;

//graphic declarations

show_point convert_to_show(simulate_point p);
simulate_point convert_to_simulate(show_point p);
void convert_show_line(simulate_point p1,simulate_point p2,int style=THIN_STYLE);
void show_stick(_stick _s,int style=THIN_STYLE);

//user defined declarations...
void _stick_gravity(_stick *_s);
void _stick_touch_floor(_stick *_s);
void _stick_keep_link(_stick *_s1,_stick *_s2);

int main()
{
    // 载入实体
    vector<_stick> _sticklist;
    _sticklist.push_back(_stick(XY_INIT,100,3,3,5,5));

    initgraph(1250,750);
    BeginBatchDraw();
    setbkcolor(RGB(255,255,255));
    setlinecolor(RGB(0,0,0));
    cleardevice();
    while(1)
    {
        // 施力
        _sticklist[0]._force(_sticklist[0]._xc,_sticklist[0]._yc,0,-1000);

        if(tick_cnt>250 and tick_cnt<260)
        {
            _sticklist[0]._force(_sticklist[0].end_point_1().x,_sticklist[0].end_point_1().y,50000,0);
        }

        // 移动
        for(int i=_sticklist.size()-1;i>=0;i--)
        {
            _sticklist[i]._move();
        }

        // 显示
        cleardevice();
        for(int i=_sticklist.size()-1;i>=0;i--)
        {
            show_stick(_sticklist[i]);
        }
        FlushBatchDraw();

        tick_cnt++;
    }
    EndBatchDraw();
}


show_point convert_to_show(simulate_point p)
{
	return show_point((int)(p.x*100),(int)(650-100*p.y));
}

simulate_point convert_to_simulate(show_point p)
{
	return simulate_point((double)p.X/(double)100,((double)650-(double)p.Y)/(double)100);
}

void convert_show_line(simulate_point _p1,simulate_point _p2,int style)
{
    int _X1=convert_to_show(_p1).X,_Y1=convert_to_show(_p1).Y,
        _X2=convert_to_show(_p2).X,_Y2=convert_to_show(_p2).Y;
    if(style!=HIDE_STYLE)
    {
        line(_X1,_Y1,_X2,_Y2);
        if(style==THICK_STYLE)
        {
            line(_X1+1,_Y1,_X2+1,_Y2);
            line(_X1-1,_Y1,_X2-1,_Y2);
            line(_X1,_Y1+1,_X2,_Y2+1);
            line(_X1,_Y1-1,_X2,_Y2-1);
        }
    }
}

void show_stick(_stick _s,int style)
{
	convert_show_line(_s.end_point_1(),_s.end_point_2(),style);
}